package org.whmmm.util.office.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

/**
 * <p>
 * <b> poi version 3.17 最低 jdk 1.6+ </b>
 * </p>
 * <p>
 * <br/><br/>
 * 创建时间: 2020/5/15 9:15
 *
 * @author whmmm
 */
public class PoiExcelUtil {

    public static Workbook getWorkBook(String filePath) {
        FileInputStream is = null;
        Workbook wb = null;
        try {


            String extString = filePath.substring(filePath.lastIndexOf("."));

            is = new FileInputStream(filePath);

            if (".xls".equals(extString)) {
                wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(extString)) {
                wb = new XSSFWorkbook(is);
            }

            if (wb == null) {
                throw new RuntimeException("excel workbook create failed!");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return wb;

    }

}
